package com.evola.rm.view.project.tree
{
	import com.evola.rm.model.Alias;
	import com.evola.rm.view.project.ProjectModel;

	import mx.collections.ListCollectionView;

	[Bindable]
	public class ResourceKeyTreeItem
	{

		public var expression:String;

		public var children:ListCollectionView;

		public var projectModel:ProjectModel;

		public function ResourceKeyTreeItem()
		{
		}

		public function getItem(currentPath:String):ResourceKeyTreeItem
		{

			//ako se trazena putanja ne nalazi u tekucoj onda nam se sigurno
			//ni element ne nalazi unutar ovog item-a
			if (currentPath.indexOf(expression) != 0)
				return null;

			var foundItem:ResourceKeyTreeItem=null;

			for each (var item:ResourceKeyTreeItem in children)
			{

				if (item.expression == currentPath)
				{
					foundItem=item;
					break;
				}

				foundItem=item.getItem(currentPath);

				if (foundItem)
					break;
			}

			return foundItem;
		}

		public function get displayName():String
		{

			var dn:String="";

			var alias:Alias=projectModel.project.getAlias(expression);

			if (alias)
				dn=alias.name;
			else
				dn=expression;

			if (children.length > 0)
				dn+=" (" + children.length + ")";

			return dn;
		}
		
		public function toString():String{
			
			return "["+expression+"]";
		}

	}
}
